{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cuda:0\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import random\n",
    "import scipy.io as scio\n",
    "import numpy as np\n",
    "import os\n",
    "import torch.nn.init as init\n",
    "from torchsummary import summary\n",
    "from torchvision import transforms\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "import torch.nn.functional as F\n",
    "device=torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "print(device)\n",
    "\n",
    "# Dir = 'G:\\科研学习\\肌电信号\\Code\\musle force\\Transed_Data\\Transformed_redacted_GIL01_Fast5.mat'\n",
    "# data = scio.loadmat(Dir)\n",
    "# print(data.keys())\n",
    "filenames = os.listdir('G:\\科研学习\\肌电信号\\Code\\musle force\\Transed_Data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义数据集\n",
    "\n",
    "transform = transforms.Compose([\n",
    "    transforms.ToTensor(),  # 将图片转换为Tensor,归一化至[0,1]\n",
    "])\n",
    "class EMGDataset(Dataset):\n",
    "    \n",
    "    def __init__(self, data, label):\n",
    "        #print(\"data.shape\")\n",
    "        #print(data.shape) #(21520, 1, 40, 10)\n",
    "        self.data = data\n",
    "        self.label = label\n",
    "        self.transforms = transform\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        emgData = self.data[index,:,:,:]\n",
    "        emgData = np.squeeze(emgData)\n",
    "        emglabel = self.label[index]\n",
    "        emglabel = emglabel.astype(np.int16)\n",
    "        emgData = self.transforms(emgData)\n",
    "        # print(\"以下是emgData\")\n",
    "        #print(emgData.shape)    #torch.Size([1, 40, 10])\n",
    "        #print(emgData)\n",
    "        return emgData,emglabel\n",
    "\n",
    "    def __len__(self):\n",
    "        #print(len(self.label))\n",
    "        return len(self.label)\n",
    "#MuscleData1返回的是整个序列\n",
    "\n",
    "class MuscleData1(Dataset):\n",
    "    def __init__(self, data, label):\n",
    "        self.data = data\n",
    "        self.label = label\n",
    "        self.transforms = transform\n",
    "        \n",
    "    def __getitem__(self,index):\n",
    "        muscle_data = self.data[index]\n",
    "        #muscle_data = np.squeeze(muscle_data)\n",
    "        print(muscle_data.shape)\n",
    "        muscle_label = self.label[index]\n",
    "        #muscle_label = np.squeeze(muscle_label)\n",
    "        muscle_data = self.transforms(muscle_data)\n",
    "        return muscle_data,muscle_label\n",
    "\n",
    "    def __len__(self):\n",
    "        return \n",
    "\n",
    "#set = MuscleData1(data['emg_rf_l'],data['mf_rf_l'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取并合并\n",
    "#尝试直接把数据相加\n",
    "Dir = 'G:\\科研学习\\肌电信号\\Code\\musle force\\Transed_Data\\\\'\n",
    "data_emg = []\n",
    "data_mf = []\n",
    "for filename in filenames:\n",
    "    data = scio.loadmat(Dir + str(filename))\n",
    "    data1 = data['emg_rf_l']\n",
    "    data1 = data1.squeeze()\n",
    "    data2 = data['mf_rf_l']\n",
    "    data2 = data2.squeeze()\n",
    "    data_emg.append(data1)\n",
    "    data_mf.append(data2)\n",
    "\n",
    "set= MuscleData1(data_emg,data_mf)\n",
    "\n",
    "\n",
    "def data_split(full_list, ratio, shuffle=True):\n",
    "    \"\"\"\n",
    "    数据集拆分: 将列表full_list按比例ratio（随机）划分为2个子列表sublist_1与sublist_2\n",
    "    :param full_list: 数据列表\n",
    "    :param ratio:     拆分比例\n",
    "    :param shuffle:   True 随机拆分，False 不随机。默认随机\n",
    "    :return:    列表1（源列表ratio比例部分）    列表2\n",
    "    \"\"\"\n",
    "    n_total = len(full_list)\n",
    "    offset = int(n_total * ratio)\n",
    "    if n_total == 0 or offset < 1:\n",
    "        return [], full_list\n",
    "    if shuffle:\n",
    "        random.shuffle(full_list)\n",
    "    sublist_1 = full_list[:offset]\n",
    "    sublist_2 = full_list[offset:]\n",
    "    return sublist_1, sublist_2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2]\n"
     ]
    }
   ],
   "source": [
    "nums = [3,2,4]\n",
    "target = 6\n",
    "for i in range(0,len(nums)):\n",
    "    for m in range(i+1,len(nums)):\n",
    "            if nums[i]+nums[m] == target:\n",
    "                a = [i,m]\n",
    "                print(a)\n",
    "\n",
    "                "
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e3b95d8d6e207246d09e19daee9bbc5bbe4502ff924268508716ea3969933742"
  },
  "kernelspec": {
   "display_name": "Python 3.7.11 ('pytorch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
